//
//  HLLiveMessageCell.m
//  YueYue
//
//  Created by hulu001 on 2024/3/12.
//

#import "HLLiveMessageCell.h"
#import "HLZegoLiveKit.h"

@interface HLLiveMessageCell ()

@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) QMUILabel *contentLabel;// 内容
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) QMUILabel *transLabel;

@end

@implementation HLLiveMessageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = UIColorClear;
        self.contentView.backgroundColor = UIColorClear;
        
        self.containerView = [[UIView alloc] init];
        _containerView.layer.masksToBounds = YES;
        _containerView.layer.cornerRadius = 8;
        [self.contentView addSubview:_containerView];
        [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.mas_equalTo(2);
            make.right.mas_lessThanOrEqualTo(0);
            make.bottom.mas_equalTo(-2);
        }];
        
        self.avatarView = [[UIImageView alloc] init];
        _avatarView.image = UIImageMake(@"rtc_message_tip");
        _avatarView.layer.masksToBounds = YES;
        _avatarView.layer.cornerRadius = 8;
        [_containerView addSubview:_avatarView];
        [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(2);
            make.size.mas_equalTo(16);
            make.top.mas_equalTo(8);
        }];
        
        self.contentLabel = [[QMUILabel alloc] qmui_initWithFont:UIFontMediumMake(12) textColor:UIColorWhite];
        _contentLabel.numberOfLines = 0;
        [_containerView addSubview:_contentLabel];
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(8);
            make.left.mas_equalTo(18);
            make.right.mas_equalTo(-12);
            make.bottom.mas_equalTo(-8);
        }];
        
        self.lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColorWhite colorWithAlphaComponent:0.15];
        [_containerView addSubview:_lineView];
        
        self.transLabel = [[QMUILabel alloc] qmui_initWithFont:UIFontMediumMake(12) textColor:UIColorWhite];
        _transLabel.numberOfLines = 0;
        _transLabel.qmui_height = 18;
        [_containerView addSubview:_transLabel];
        _lineView.hidden = YES;
        _transLabel.hidden = YES;
        
    }
    return self;
}


- (void)setMessageModel:(HLLiveMessageModel *)messageModel
{
    _messageModel = messageModel;
    _lineView.hidden = YES;
    _transLabel.hidden = YES;
    _avatarView.hidden = YES;
    if ([messageModel.cid integerValue] == 1000){
        _containerView.backgroundColor = [UIColorBlack colorWithAlphaComponent:0.4];
        _contentLabel.attributedText = [self attrTextWithAll:messageModel.ext.origin high:messageModel.ext.origin];
        [_contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(8);
            make.left.mas_equalTo(12);
            make.right.mas_equalTo(-12);
            make.bottom.mas_equalTo(-8);
        }];
    } else {
        [_avatarView sd_setImageWithURL:[NSURL URLWithString:messageModel.fromAvatar]];
        _containerView.backgroundColor = [UIColorBlack colorWithAlphaComponent:0.4];
        _contentLabel.attributedText = [self attrTextWithAll:[NSString stringWithFormat:@"%@: %@", messageModel.fromName, messageModel.ext.origin] high:messageModel.fromName];
        
        if (![messageModel.from isEqualToString:HLZegoLiveKit.sharedInstance.female.femaleUserId]){
//            _lineView.hidden = NO;
//            _transLabel.hidden = NO;
//            _transLabel.text = messageModel.transText?:@"翻译中...";
            [_contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(8);
                make.left.mas_equalTo(12);
                make.right.mas_equalTo(-12);
                make.bottom.mas_equalTo(-8);
            }];
//            [_lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.left.right.equalTo(_contentLabel);
//                make.height.mas_equalTo(1);
//                make.top.equalTo(_contentLabel.mas_bottom).offset(4);
//            }];
//
//            [_transLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.left.right.equalTo(_contentLabel);
//                make.top.equalTo(_lineView.mas_bottom).offset(4);
//                make.bottom.mas_equalTo(-4);
//            }];
            //[self transMessage];
        } else {
            _avatarView.hidden = NO;
            [_contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(8);
                make.left.mas_equalTo(18);
                make.right.mas_equalTo(-12);
                make.bottom.mas_equalTo(-8);
            }];
        }
    }
}

- (NSAttributedString *)attrTextWithAll:(NSString *)all high:(NSString *)high
{
    NSString *string = all;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:3.8];
    paragraphStyle.alignment = NSTextAlignmentLeft;
    
    NSDictionary *normal = @{NSFontAttributeName:UIFontMediumMake(12), NSForegroundColorAttributeName:UIColorWhite, NSParagraphStyleAttributeName:paragraphStyle};
    NSDictionary *highDic = @{NSFontAttributeName:UIFontMediumMake(12), NSForegroundColorAttributeName:UIColorMakeWithHex(@"#FF0ECAF4"), NSParagraphStyleAttributeName:paragraphStyle};
    NSRange range1 = [string rangeOfString:high];
    
    NSMutableAttributedString *mutableString = [NSMutableAttributedString.alloc init];
    [mutableString appendAttributedString:[NSAttributedString.alloc initWithString:string attributes:normal]];
    [mutableString setAttributes:highDic range:range1];
    
    if ([_messageModel.cid integerValue] == 112 && _messageModel.ext.gImage){
        NSTextAttachment *imageAttachment = [[NSTextAttachment alloc] init];
        imageAttachment.image = _messageModel.ext.gImage;
        imageAttachment.bounds = CGRectMake(-2, -2, 14, 14);
        NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:imageAttachment];
        NSAttributedString *numString = [[NSAttributedString alloc] initWithString:@"x1"];
        [mutableString appendAttributedString:imageString];
        [mutableString appendAttributedString:numString];
    }
    
    return mutableString;
}

@end
